home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / shells / kiss-0.11 / kiss-0 / kiss / src / dowhere.c < prev    next >
C/C++ Source or Header  |  1995-03-23  |  2KB  |  82 lines

  1. #include "kiss.h"
  2.  
  3. static int findexecutable (char *dir, Stringstack what)
  4. {
  5.     register int
  6.     i,
  7.     hits = 0;
  8.     char
  9.     buf [FILENAMELEN];
  10.     struct stat
  11.     statbuf;
  12.  
  13.     for (i = 1; i < what.nstr; i++)
  14.     {
  15.     strcpy (buf, dir);
  16.     if (buf [strlen (buf) - 1] != '/')
  17.         strcat (buf, "/");
  18.     strcat (buf, what.str [i]);
  19.  
  20.     if (! stat (buf, &statbuf) && (statbuf.st_mode & S_IXUSR))
  21.     {
  22.         printf ("%s\n", buf);
  23.         hits++;
  24.     }
  25.     }
  26.  
  27.     return (hits);
  28. }
  29.     
  30.  
  31. int dowhere (Stringstack s)
  32. {
  33.     char
  34.     buf [FILENAMELEN];
  35.     register char
  36.     *cp;
  37.     register int
  38.     aliasnr,
  39.     j,
  40.     i,
  41.     res = 0;
  42.  
  43.     if (getopt (s.nstr, s.str, "h") != -1 || s.nstr == 1)
  44.     error ("Bad commandline.\n"
  45.            "Usage: %s [-h] program(s)\n"
  46.            "Where:\n"
  47.            "    -h: this text\n"
  48.            "    program(s): programs to locate on PATH or as built-in\n"
  49.            , progname, progname);
  50.  
  51.     for (i = 1; i < s.nstr; i++)
  52.     {
  53.     if ( (aliasnr = isalias (s.str [i])) != -1 )
  54.     {
  55.         printf ("%s is aliased to ", s.str [i]);
  56.         for (j = 1; j < alias [aliasnr].nstr; j++)
  57.         printf ("%s ", alias [aliasnr].str [j]);
  58.         putchar ('\n');
  59.         res++;
  60.     }
  61.     if (isinternal (s.str [i]) != -1)
  62.     {
  63.         printf ("%s is a built-in command\n", s.str [i]);
  64.         res++;
  65.     }
  66.     }
  67.  
  68.     if (! (cp = getenv ("PATH")) )
  69.     return (warning ("cannot determine PATH setting"));
  70.  
  71.     strcpy (buf, cp);
  72.  
  73.     if ( (cp = strtok (buf, ":")) )
  74.     {
  75.     res += findexecutable (cp, s);
  76.     while ( (cp = strtok (NULL, ":")) )
  77.         res += findexecutable (cp, s);
  78.     }
  79.  
  80.     return (res == 0);
  81. }
  82.